<%@ page language="java" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html"> 
<html>
	<body bgColor=#f7f7f7>
		<%@page language="java" import="java.net.*,java.io.*"%>
		<%
			String filePath=request.getAttribute("filePath").toString();//要下载的文件的路径
			String fileName=request.getAttribute("fileName").toString();//下载的文件的保存的文件名
			String isdel=request.getAttribute("isdel").toString();//是否删除源文件，若为isdel则删除源文件
			isdel=null!=isdel && !"".equals(isdel) ? isdel.trim() : "";
			request.setCharacterEncoding("UTF-8");
			String filenamedownload = filePath;
			response.reset();//可以加也可以不加
			response.setContentType("application/x-download");

			String filedisplay =fileName;
			filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
			response.addHeader("Content-Disposition", "attachment;filename="
					+ filedisplay);

			OutputStream outp = null;
			FileInputStream in = null;
			try {
				outp = response.getOutputStream();
				in = new FileInputStream(filenamedownload);
				byte[] b = new byte[1024];
				int i = 0;
				while ((i = in.read(b)) > 0) {
					outp.write(b, 0, i);
				}
				outp.flush();
				//此处是处理getoutputstream()方法与inputstream并存时必报的异常
				out.clear(); 
				out = pageContext.pushBody();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (null != in) {
					in.close();
					in = null;
				}
				if (null != outp) {
					outp.close();
					outp = null;
				}
			if(isdel.equals("isdel")){//判断是否删除源文件
				File f = new File(filenamedownload); //path:E:/tomcat/...  
				try {
					f.delete();
					f.deleteOnExit();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		%>
	</body>
</html>
